package cn.hinglo.gameclaw.client.action;

import cn.hinglo.gameclaw.client.service.SignService;
import cn.hinglo.gameclaw.data.controller.BaseController;
import cn.hinglo.gameclaw.data.entity.Result;
import cn.hinglo.gameclaw.user.bean.Sign;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @createTime: 2018/1/2
 * @author: HingLo
 * @description: 签到处理
 */
@RestController
@RequestMapping("/client/s/")
public class SignAction extends BaseController {
    private final SignService signService;


    @Autowired
    public SignAction(SignService signService) {
        this.signService = signService;
    }


    /**
     * 签到记录查询
     *
     * @return 操作结果
     */
    @GetMapping("list")
    public Result listSign() {
        String userId = getUserId(request);
        return signService.listSignService(userId);
    }


    /**
     * 添加签到记录
     * * @param sign 签到信息
     *
     * @param bindingResult 错误信息绑定结果对象
     * @return 操作结果
     */
    @PostMapping("add")
    public Result addSign(@Valid Sign sign, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return error(bindingResult.getFieldError().getDefaultMessage());
        }
        String userId = getUserId(request);
        sign.setUserId(userId);
        return signService.addSignService(sign);
    }


    /***
     * 检测今天是否已经签到过了
     * @return 操作结果
     */
    @GetMapping("check")
    public Result checkSign() {
        String userId = getUserId(request);
        if (StrUtil.isNotBlank(userId)) {
            return success(signService.checkSignService(userId));
        }
        return error("用户Id格式不正确");
    }


}
